package com.pencil.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.pencil.dao.ibatis.SqlDaoSuport;
import com.pencil.vo.UserVo;

public class UserDao extends SqlDaoSuport implements IUserDao {

	private Logger logger = Logger.getLogger(this.getClass());
	public UserDao() {
		super();
	}

	public UserDao(String path) {
		super(path);
	}

	public List<UserVo> getAllUser() {
		List<UserVo> userVo = new ArrayList<UserVo>();
		try {
			userVo = this.getSqlMapClient().queryForList("User.getAllUser");
			logger.info("getAllUser loading Success!!");
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}
		return userVo;
	}

	public UserVo getAUser(String user_id) {
		UserVo userVo = new UserVo();
		try {
			userVo = (UserVo) this.getSqlMapClient().queryForObject("User.getAUser", user_id);
			logger.info("get a user Success!!");
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}
		return userVo;
	}

	public int regiUser(UserVo userVo) {
		int result = 1;
		try {
			this.getSqlMapClient().insert("User.regiUser", userVo);
			logger.info(userVo.getUser_id() + " register success!!");

		} catch (SQLException e) {
			result = -1;
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	public int updateUser(String user_id) {
		int result = 1;
		try {
			this.getSqlMapClient().update("User.updateUser", user_id);
			logger.info(user_id + " update Success!!");
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}
		return result;
	}

	public int goOutUser(String user_id) {
		int result = 1;
		try {
			this.getSqlMapClient().update("User.goOutUser", user_id);
			logger.info(user_id + " goOut Success!!");
		} catch (SQLException e) {
			logger.error(e.getMessage(), e);
		}
		return result;
	}

}
